1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| public static void solve() { int n = io.nextInt(); char[] r = io.next().toCharArray(); char[] c = io.next().toCharArray();
int[] row = new int[n]; int[] col = new int[n]; char[][] ans = new char[n][n]; for (int i = 0; i < n; i++) { Arrays.fill(ans[i], '.'); } boolean ok = dfs(0, 0, r, c, ans, row, col, 0);
if (!ok) { io.println("No"); return; } io.println("Yes"); for (int i = 0; i < n; i++) { io.println(new String(ans[i])); } }
private static boolean dfs(int x, int y, char[] r, char[] c, char[][] ans, int[] row, int[] col, int cnt) { int n = r.length; if (x == n) { return cnt == 3 * n; } if (n - 1 - y >= 3 - Integer.bitCount(row[x])) { if (dfs(x + (y + 1) / n, (y + 1) % n, r, c, ans, row, col, cnt)) { return true; } } for (int i = 0; i < 3; i++) { if ((row[x] >> i & 1) == 1 || (col[y] >> i & 1) == 1) { continue; } if (row[x] == 0 && r[x] != 'A' + i) { continue; } if (col[y] == 0 && c[y] != 'A' + i) { continue; } row[x] |= 1 << i; col[y] |= 1 << i; ans[x][y] = (char) ('A' + i); if (dfs(x + (y + 1) / n, (y + 1) % n, r, c, ans, row, col, cnt + 1)) { return true; } row[x] ^= 1 << i; col[y] ^= 1 << i; ans[x][y] = '.'; } return false; }
|